//
//  AppendDetailsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/5.
//

#import "AppendDetailsViewController.h"
#import "AppendDetailsHeaderView.h"
#import "AppendDetailsModel.h"
#import "AppendDetailsTableViewCell.h"
#import "AppendSettingShowView.h"
#import "AppendXGFishpondViewController.h"
#import "AppendMultipleFishpondViewController.h"
#import "AddPondModel.h"
#import "AppendLogsViewController.h"
#import "YCMenuView.h"
#import "YTTipsShowView.h"
#import "AppendPostponeViewController.h"

@interface AppendDetailsViewController ()
@property (nonatomic ,strong) AppendDetailsModel * model;
@property (nonatomic ,strong) UILabel * leftlab;
@property (nonatomic ,strong) UIButton * zdspBtn;

@end

@implementation AppendDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:self.titleStr andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    self.leftlab= leftlab;
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    UIButton * zdspBtn = [UIButton buttonWithType:0];
    self.zdspBtn = zdspBtn;
    zdspBtn.addimg([UIImage imageNamed:@"more_function"]).addNormalColor(UIColor.whiteColor).addFont(kCFont(18)).addTarget(self,@selector(openSZ));
    [topview addSubview:zdspBtn];
    [zdspBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.centerY.offset(KTop/2);
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(topview.mas_bottom);
    }];
    
    [self loadNewData];
}
- (void)openSZ{
    NSMutableArray * array = [NSMutableArray array];
    MJWeakSelf
    YCMenuAction * m = [YCMenuAction actionWithTitle: @"设置" image:[UIImage imageNamed:@"set_up"] handler:^(YCMenuAction *action) {
        [weakSelf openSZXX];
    }];
    [array addObject:m];
    if(self.model.isPostponed > 0){///是否延期 0否 1是
        YCMenuAction * m1 = [YCMenuAction actionWithTitle: @"继续下发" image:[UIImage imageNamed:@"continue to distribute"] handler:^(YCMenuAction *action) {
            [YTTipsShowView showviewWithview:^(UIView * _Nonnull view) {
                UILabel * toplab = [DTKTool labelWithText:@"确定继续下发任务?" andTextColor:KcolorHex(@"#1E2425") andFont:kCFont(18)];
                [view addSubview:toplab];
                [toplab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.offset(0);
                    make.top.offset(kwt(35));
                }];
                UILabel * contentlab = [DTKTool labelWithText:@"任务将于次日凌晨 0 点准时自动下发" andTextColor:KcolorHex(@"#898989") andFont:kFont(15)];
                contentlab.textAlignment = NSTextAlignmentCenter;
                contentlab.numberOfLines = 0;
                [view addSubview:contentlab];
                [contentlab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.offset(0);
                    make.top.offset(kwt(72));
                }];
                
            } andviewHight:kwt(190) andLeftTitle:@"取消" andRightTitle:@"确定" andLeftBlock:^{
                
            } andRightBlock:^{
                [weakSelf continuePondTask];
            }];
        }];
        [array addObject:m1];
    } else {
        YCMenuAction * m1 = [YCMenuAction actionWithTitle: @"延期下发" image:[UIImage imageNamed:@"continue to distribute"] handler:^(YCMenuAction *action) {
            [YTTipsShowView showviewWithview:^(UIView * _Nonnull view) {
                UILabel * toplab = [DTKTool labelWithText:@"确定延期下发任务?" andTextColor:KcolorHex(@"#1E2425") andFont:kCFont(18)];
                [view addSubview:toplab];
                [toplab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.offset(0);
                    make.top.offset(kwt(35));
                }];
                UILabel * contentlab = [DTKTool labelWithText:@"开启后，系统将暂停下发任务，且批\n量创建的池塘任务将独立执行，不再\n与其他池塘任务批量同步" andTextColor:KcolorHex(@"#898989") andFont:kFont(15)];
                contentlab.textAlignment = NSTextAlignmentCenter;
                contentlab.numberOfLines = 0;
                [view addSubview:contentlab];
                [contentlab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.offset(0);
                    make.top.offset(kwt(72));
                }];
                
            } andviewHight:kwt(228) andLeftTitle:@"取消" andRightTitle:@"确定" andLeftBlock:^{
                
            } andRightBlock:^{
                [weakSelf postponing];
            }];
        }];
        [array addObject:m1];
        
   
    }
    ///是否有延期记录 0否 1是
    if(self.model.isHavePostponed > 0){
        YCMenuAction * m2 = [YCMenuAction actionWithTitle: @"查看延期" image:[UIImage imageNamed:@"view_delay"] handler:^(YCMenuAction *action) {
            AppendPostponeViewController * vc = [AppendPostponeViewController new];
            vc.pondId = weakSelf.pondId;
            vc.pondName = weakSelf.model.name;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        [array addObject:m2];
    }
  
    YCMenuView * menuview = [YCMenuView menuWithActions:array width:kwt(135) relyonView:self.zdspBtn];
    menuview.menuColor = KcolorHex(@"#4C4C4C");
    menuview.textColor = UIColor.whiteColor;
    menuview.textFont = kFont(14);
    menuview.separatorColor = KcolorHex(@"#636161");
    menuview.disMissBlock = ^{
        
    };
    [menuview show];
}

///延期下发
- (void)postponing{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondId forKey:@"id"];
    [SCNetManger PostponingWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self loadNewData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

///继续下发
- (void)continuePondTask{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondId forKey:@"id"];
    [SCNetManger ContinuePondTaskWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self loadNewData];          
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)openSZXX {
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondId forKey:@"id"];
    [SCNetManger PostPondManageIsGroupWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            MJWeakSelf
            AddXGPondModel * tempModel = [AddXGPondModel yy_modelWithDictionary:model.result];
            if(tempModel.isGroup){
                [AppendSettingShowView showViewWithType:0 oneBlock:^{
                    AppendXGFishpondViewController * vc = [AppendXGFishpondViewController new];
                    vc.ID = weakSelf.pondId;
                    vc.isGroup = YES;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                } andallBlock:^{
                    AppendXGFishpondViewController * vc = [AppendXGFishpondViewController new];
                    vc.ID = weakSelf.pondId;
                    vc.isAll = YES;
                    vc.isGroup = YES;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
            } else {
                AppendXGFishpondViewController * vc = [AppendXGFishpondViewController new];
                vc.ID = weakSelf.pondId;
                vc.isGroup = NO;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}

- (void)loadNewData{
//    
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondId forKey:@"id"];
    [SCNetManger PostPondManageBasicInfoWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            self.model = [AppendDetailsModel yy_modelWithDictionary:model.result];
            self.leftlab.text = self.model.name;
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
   
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    AppendDetailsHeaderView * headerview = [AppendDetailsHeaderView initview];
    MJWeakSelf
    headerview.openRZBlock = ^(int type) {
        AppendLogsViewController * vc = [AppendLogsViewController new];
        vc.pondname = weakSelf.model.name;
        vc.pondId = weakSelf.model.ID;
        vc.type = type;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    headerview.model = self.model;
    return headerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(self.model.typeCode == 1){
        return kwt(173 - 16) + self.model.breedTypeH;
    } else {
        return kwt(153);
    }
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(self.model.typeCode == 1){
        return kwt(350);
    } else {
        return kwt(320);
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    AppendDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AppendDetailsTableViewCell"];
    if (cell == nil) {
        cell = [[AppendDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppendDetailsTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.model;
    return cell ;
    
}
@end
